package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.global.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.time.LocalDateTime;
import java.util.*;


@Slf4j
@Service
public class EmpServiceimp<EmpMapper> implements EmpService {
    @Autowired
    private EmpLogService empLogService;
    @Autowired

    private com.itheima.mapper.EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
//        PageHelper.startPage(page, pageSize);
//        List<Emp> empList = empMapper.list(name,gender,begin,end);
//        Page<Emp> p = (Page<Emp>) empList;
//        return new PageResult<Emp>(p.getTotal(),p.getResult());
//    }


//    @Override
//    public PageResult<Emp> page(Integer page,Integer pageSize) {
//        PageHelper.startPage(page,pageSize);
//        List<Emp> empList = empMapper.list();
//        Page<Emp> p = (Page<Emp>) empList;
//        return new PageResult<Emp>(p.getTotal(),p.getResult());
//    }
//分页查询
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        List<Emp> empList = empMapper.list(empQueryParam);
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult<Emp>(p.getTotal(), p.getResult());
    }
//添加员工
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void save(Emp emp) {
        try {
            //添加当前时间
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);

            List<EmpExpr> list = emp.getExprList();

            if (!CollectionUtils.isEmpty(list)) {
                list.forEach(empExpr -> {
                    empExpr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(list);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "新增员工" + emp);
            empLogService.insertLog(empLog);
        }
    }
    //删除
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        //删除员工基本信息
        empMapper.deleteByIds(ids);

        //删除员工工作经历
        empExprMapper.deleteByEmpIds(ids);
    }
//数据回显
    @Override
    public Emp getInfo(Integer id) {
        Emp emp = empMapper.getById(id);
        return emp ;
    }
    //修改数据
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Emp emp) {
        //根据id修改基本信息

        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //删除工作经历
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //添加工作经历
        List<EmpExpr> list = emp.getExprList();
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(list);
        }

    }
//？ 登录
    @Override
    public LoginInfo login(Emp emp) {
        //调用mapper接口
      Emp e =   empMapper.selectByUsernameAndPassword(emp);
        //判断是否存在员工
        if(e != null){
            log.info("登录成功 {}",e);
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            //生成jwt令牌
           String jwt = JwtUtils.generateJwt(claims);
            return new LoginInfo(e.getId(),e.getUsername(),e.getName(),jwt);
        }
        //返回null(不存在)
    return null;

    }
}





